import axios from 'axios';

const instance = axios.create({
  timeout: 20000,
});

instance.interceptors.request.use(
  config => {
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

instance.interceptors.response.use(
  response => {
    return response.data;
  },
  err => {
    return err;
  }
);

export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    instance({
      method: 'GET',
      url,
      params,
    })
      .then(response => {
        resolve(response);
      })
      .catch(err => {
        reject(err);
      });
  });
}

export function post(url, params = {}) {
  return new Promise((resolve, reject) => {
    instance({
      method: 'POST',
      url,
      data: params,
    })
      .then(response => resolve(response))
      .catch(err => reject(err));
  });
}
